﻿using System.Collections.Generic;
using System.Linq;
using Configuration;
using Model;
using Repository.Common;

namespace Repository
{
    public class tkntProductRepository : Repository<tkntProduct>
    {
        public tkntProductRepository(Database context)
            : base(context)
        {
        }

        public List<tkntProduct> GetAllHaveCategory()
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = from r in GetQuery().Include("Category").OrderByDescending(o => o.Id) select r;

            return res.ToList();
        }

        public List<tkntProduct> GetAllByProductID(int ProductID)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().Include("Images").OrderByDescending(o => o.Id) where c.Id == ProductID select c).ToList();

            return res.ToList();
        }

        public List<tkntProduct> GetAllByCategoryIDNoImage(int CategoryID)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().OrderByDescending(o => o.Id) where c.Category.Id == CategoryID select c).ToList();

            return res.ToList();
        }

        public tkntProduct GetById(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from r in GetQuery().Include("Category").OrderBy(o => o.Id) where r.Id == id select r).SingleOrDefault();

            return res;
        }

        public List<tkntProduct> Paging(int pageIndex, int numOfRows)
        {
            var skipRows = (pageIndex - 1) * numOfRows;

            return GetAllHaveCategory().Skip(skipRows).Take(numOfRows).ToList();
        }

        public tkntProduct HaveImage(int id)
        {
            GetContext().ContextOptions.LazyLoadingEnabled = true;

            var res = (from c in GetQuery().Include("Images") where c.Id == id select c).SingleOrDefault();

            return res;
        }

        public List<tkntProduct> SearchByDescription(string description, string locale)
        {
            var res = new List<tkntProduct>();
            if (locale.ToLower() == "vi")
                res = (from c in GetQuery() where c.ViDescription.ToLower().Contains(description.ToLower()) select c).ToList();
            else
                res = (from c in GetQuery() where c.EnDescription.ToLower().Contains(description.ToLower()) select c).ToList();

            return res;
        }
    }
}